#load "nums.cma";;
#load "gencode.cmo";;
#load "clexer.cmo";;
#load "cparser.cmo";;
#load "cio.cmo";;

open Gencode.ClangExec;;

let parse infile = 
let lexbuf = Lexing.from_channel (open_in infile) in 
 (Cparser.s Clexer.token lexbuf);;

let error_pos infile =
  let lexbuf = Lexing.from_channel (open_in infile) in 
    try (Cparser.s Clexer.token lexbuf) with 
        Parsing.Parse_error -> 
          let loc_start = Lexing.lexeme_start lexbuf in
          let loc_end = Lexing.lexeme_end lexbuf in
            Printf.printf "at position %d %d\n" loc_start loc_end;
            raise  (Parsing.Parse_error) ;;


(*-------------------------------------Main program-------------------------------------------------------------*)

let hol_eq_record = {Gencode.HOL.equal = (=)} ;;

let check_wf_program infile = 
   let prog = error_pos infile in  
     print_string (Cio.iprint_full_program prog);
     if wf_prog hol_eq_record hol_eq_record hol_eq_record prog then print_string ("\nProgram '"^infile^"' is well-formed\n")
        else print_string ("\nProgram '"^infile^"' is not well-formed\n");
;;

check_wf_program "Peterson.txt"
